//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using OpenLS.Core.Formatting;

namespace OpenLS.Core
{
    ///<summary>
    /// Format for text
    ///</summary>
    public class TextFormat : Format
    {
        public TextFormat(string code)
            : base(code)
        {
            
        }
        string _stringCode;
        private string _rightStringCode;
        private char? repeatChar;

        public override object GetFormattedValue(object o, IFormatValueConverter converter)
        {
            if (o == null)
                return string.Empty;
            if (repeatChar != null)
            {
                if (!string.IsNullOrEmpty(_rightStringCode))
                    return new CompositeFormattedValue(new List<object>()
                                                           {
                                                               string.Format(_stringCode, o),
                                                               new RepeatText(repeatChar.Value.ToString()),
                                                               string.Format(_rightStringCode, o)
                                                           });
                return new CompositeFormattedValue(new List<object>()
                                                       {
                                                           string.Format(_stringCode, o),
                                                           new RepeatText(repeatChar.Value.ToString())
                                                       });
            }

            return string.Format(_stringCode, o);
        }

        public override bool CanFormat(object o)
        {
            return true;
        }

        internal override void Analyze(List<FormatToken> list)
        {
            _stringCode = "";
            foreach (FormatToken token in list)
            {
                if (token is ExplicitStringToken)
                {
                    if (repeatChar != null)
                        _rightStringCode += "{0}";
                    else
                        _stringCode += "{0}";
                    continue;
                }
                if (token is RepeatToken)
                {
                    this.repeatChar = ((RepeatToken) token).Value;
                    _rightStringCode = "";
                    continue;

                }
                if (token is ClauseFormatToken)
                {
                    if (repeatChar != null)
                        _rightStringCode += token.Code;
                    else
                        _stringCode += token.Code;
                    continue;
                }
                if (token is ClauseSeparatorToken)
                {
                    continue;
                }
                Warn();
            }
        }
        public override FormatCategory Category
        {
            get { return FormatCategory.Text; }
        }

    }
}
